package net.wexoo.onlinedivelog.reusable.modalwindow;

import net.wexoo.onlinedivelog.reusable.page.WEXWebPage;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;

public abstract class WEXModalWindow<T> extends ModalWindow {

  private static final long serialVersionUID = 1L;
  protected WEXWebPage windowContent;

  public WEXModalWindow(String id) {
    super(id);

    setPageCreator(new ModalWindow.PageCreator() {

      private static final long serialVersionUID = 1L;

      @Override
      public Page createPage() {
        windowContent = returnPageToBeCreated();
        return windowContent;
      }
    });
    setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {

      private static final long serialVersionUID = 1L;

      @Override
      public void onClose(AjaxRequestTarget target) {
        onCloseAction(target);
      }
    });
  }


  public void loadWithNewBean() {
    setPageCreator(new ModalWindow.PageCreator() {

      private static final long serialVersionUID = 1L;

      @Override
      public Page createPage() {
        windowContent = returnPageToBeCreated();
        return windowContent;
      }
    });
  }

  // protected void loadById(final Long id) {
  // setPageCreator(new ModalWindow.PageCreator() {
  //
  // private static final long serialVersionUID = 1L;
  //
  // @Override
  // public Page createPage() {
  // return returnPageToInitialise(id);
  // }
  // });
  // }

  public void loadByBean(final T bean) {
    setPageCreator(new ModalWindow.PageCreator() {

      private static final long serialVersionUID = 1L;

      @Override
      public Page createPage() {
        windowContent = returnPageToBeCreated(bean);
        return windowContent;
      }
    });
  }

  protected WEXWebPage returnPageToBeCreated() {
    return returnPageToBeCreated(null);
  }

  // protected abstract Page returnPageToInitialise(Long id);

  protected abstract WEXWebPage returnPageToBeCreated(T bean);

  protected abstract void onCloseAction(AjaxRequestTarget target);
}
